home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectPlay / Maze / MazeCommon / Random.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  1.8 KB  |  67 lines

  1. //----------------------------------------------------------------------------
  2. // File: random.h
  3. //
  4. // Desc: see main.cpp
  5. //
  6. // Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #ifndef _RANDOM_H
  9. #define _RANDOM_H
  10.  
  11.  
  12.  
  13.  
  14.  
  15. //-----------------------------------------------------------------------------
  16. // Name: 
  17. // Desc: Random number generator class - a simple linear congruential generator.
  18. // We use this instead of the CRT function because we want to be certain that
  19. // we are using the exact same generator on both server and client side and so
  20. // (a) don't want to be at the mercy of CRT version changes, and (b) may want
  21. // multiple independent generators which we can rely on the sequencing of.
  22. //-----------------------------------------------------------------------------
  23. class   CRandom
  24. {
  25. public:
  26.     // Constructor. The random formula is X(n+1) = (a*X(n) + b) mod m
  27.     // The default values for a,b,m give a maximal period generator that does not
  28.     // overflow with 32-bit interger arithmetic. 
  29.     CRandom( DWORD seed = 31415, DWORD a = 8121, 
  30.              DWORD b = 28411,    DWORD m = 134456 ) :
  31.         m_dwSeed(seed),m_dwA(a),m_dwB(b),m_dwM(m) {};
  32.  
  33.     // Grab a random DWORD between 0 and m
  34.     DWORD Get()
  35.     {
  36.         m_dwSeed = ((m_dwA*m_dwSeed)+m_dwB) % m_dwM;
  37.         return m_dwSeed;
  38.     };
  39.  
  40.     // Grab a random DWORD in the range [0,n-1]
  41.     DWORD Get( DWORD n )
  42.     {
  43.         return (Get() % n);
  44.     };
  45.  
  46.     // Grab a random float in the range [0,1]
  47.     float GetFloat()
  48.     {
  49.         return Get() / float(m_dwM-1);
  50.     };
  51.  
  52.     // Reset the seed
  53.     void Reset( DWORD seed = 31415 )
  54.     {
  55.         m_dwSeed = seed;
  56.     };
  57.  
  58. protected:
  59.     DWORD m_dwA, m_dwB, m_dwM;
  60.     DWORD m_dwSeed;
  61. };
  62.  
  63.  
  64.  
  65.  
  66. #endif
  67.